package www.lagou.app;

import www.lagou.dao.AccountDao;
import www.lagou.dao.TransactionDao;
import www.lagou.entity.Account;
import www.lagou.entity.Transaction;
import www.lagou.utils.DateUtils;

import java.sql.SQLException;

public class Test1 {

    public static void main(String[] args) {
        AccountDao accountDao = new AccountDao();
        TransactionDao transactionDao = new TransactionDao();

        double money = 5000;
        Account account = null;
        Account account1 = null;
        Transaction ta = new Transaction();
        Transaction ta1 = new Transaction();
        try {
            account = accountDao.findByCard("1122334455");//查询卡号为1122334455信息
            account1 = accountDao.findByCard("55443332211");//查询卡号为5544332211信息
            if (account != null && account1 != null) {
                System.out.println("查询结果：");
                System.out.println(account.toString());
                System.out.println(account1.toString());

                if(account.getBalance()>money){
                    account.setBalance(account.getBalance() - money);//余额减少5000
                    account1.setBalance(account1.getBalance() + money);//余额增加5000

                    if (accountDao.accountUpdate(account) >0 && accountDao.accountUpdate(account1) > 0){
                        System.out.println("转账成功");
                        ta.setCardid(account.getCard());
                        ta.setTramoney(money);
                        ta.setTradate(DateUtils.getDateFormart());
                        ta.setTratype("转出");

                        ta1.setCardid(account1.getCard());
                        ta1.setTramoney(money);
                        ta1.setTradate(DateUtils.getDateFormart());
                        ta1.setTratype("转入");

                        if (transactionDao.transactionInsert(ta)>0&&transactionDao.transactionInsert(ta1)>0){
                            System.out.println("日志保存成功");
                        }
                        account = accountDao.findByCard("1122334455");//查询卡号为1122334455信息
                        account1 = accountDao.findByCard("55443332211");//查询卡号为5544332211信息
                        if (account != null && account1 != null) {
                            System.out.println("再次查询结果：");
                            System.out.println(account.toString());
                            System.out.println(account1.toString());
                        }
                    }else {
                        System.out.println("转账失败");
                    }
                }else {
                    System.out.println("余额不足！");
                }
            }else {
                System.out.println("输入的卡号不存在");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
